Skip to main content

Week 11 — Graph Algorithms

1. Longest Path​

For a weighted graph, the length of a path is the sum of the weights of its edges.

For a path

v0→v1→⋯→vkv_0\rightarrow v_1\rightarrow\cdots\rightarrow v_k

its total weight is

∑i=0k−1w(vi,vi+1)\sum_{i=0}^{k-1}w(v_i,v_{i+1})

The longest path is a path with maximum total weight among the paths being considered.


2. Longest Path in a DAG​

For a directed acyclic graph (DAG), longest paths can be found using a topological ordering.

For an edge

u→vu\rightarrow v

the longest-path value can be updated as

d[v]=max⁡(d[v],d[u]+w(u,v))d[v] = \max(d[v],d[u]+w(u,v))

Processing vertices in topological order ensures that the required predecessor values have already been computed.


Transitive Closure

3. Reachability​

A vertex vv is reachable from vertex uu if there exists a path from uu to vv.


4. Transitive Closure​

The transitive closure of a directed graph represents reachability between every pair of vertices.

For vertices ii and jj,

Tij={1if j is reachable from i0otherwiseT_{ij} = \begin{cases} 1 & \text{if }j\text{ is reachable from }i\\ 0 & \text{otherwise} \end{cases}

5. Transitive Closure Using Boolean Matrices​

The adjacency matrix can be extended to represent paths of greater length.

Matrix operations can therefore be used to determine reachability.


Matrix Multiplication

6. Matrix Product​

For matrices AA and BB, the product C=ABC=AB is defined by

Cij=∑kAikBkjC_{ij} = \sum_{k}A_{ik}B_{kj}

7. Matrix Dimensions​

If

A∈Rm×nA\in\mathbb{R}^{m\times n}

and

B∈Rn×pB\in\mathbb{R}^{n\times p}

then

AB∈Rm×pAB\in\mathbb{R}^{m\times p}

8. Matrix Multiplication and Graphs​

For an adjacency matrix AA, powers of the matrix can represent paths of different lengths.

The entry

(Ak)ij(A^k)_{ij}

can be used to determine the number of walks of length kk from vertex ii to vertex jj.


Single-Source Shortest Paths

9. Shortest Path​

The shortest path between two vertices is a path with minimum total edge weight.

For a source vertex ss, the single-source shortest-path problem finds the shortest distance from ss to every reachable vertex.


10. Relaxation​

For an edge

u→vu\rightarrow v

with weight w(u,v)w(u,v), relaxation checks whether going through uu gives a shorter path to vv.

d[v]>d[u]+w(u,v)d[v] > d[u]+w(u,v)

If so, update:

d[v]=d[u]+w(u,v)\boxed{ d[v]=d[u]+w(u,v) }

Dijkstra's Algorithm

11. Dijkstra's Algorithm​

Dijkstra's algorithm solves the single-source shortest-path problem for graphs with non-negative edge weights.


12. Dijkstra Initialization​

For source vertex ss:

d[s]=0d[s]=0

For every other vertex vv:

d[v]=∞d[v]=\infty

13. Dijkstra's Algorithm​

Repeatedly:

  1. Select the unprocessed vertex with the smallest tentative distance.
  2. Mark it as processed.
  3. Relax its outgoing edges.
  4. Continue until all required vertices are processed.

14. Dijkstra Complexity​

Using an adjacency list and priority queue, a common implementation has complexity

O((∣V∣+∣E∣)log⁡∣V∣)O((|V|+|E|)\log |V|)

Bellman-Ford Algorithm

15. Bellman-Ford Algorithm​

Bellman-Ford solves the single-source shortest-path problem and can handle negative edge weights.

It can also detect reachable negative-weight cycles.


16. Bellman-Ford Relaxation​

All edges are repeatedly relaxed.

For a graph with ∣V∣|V| vertices, the edges are relaxed ∣V∣−1|V|-1 times.


17. Negative Cycle Detection​

After ∣V∣−1|V|-1 relaxation passes, perform one additional pass.

If any distance can still be improved, a reachable negative-weight cycle exists.


18. Bellman-Ford Complexity​

The time complexity is

O(∣V∣∣E∣)\boxed{ O(|V||E|) }

All-Pairs Shortest Paths

19. All-Pairs Shortest Paths​

The all-pairs shortest-path problem finds the shortest distance between every pair of vertices.

The result can be represented by a distance matrix DD:

Dij=shortest distance from i to jD_{ij} = \text{shortest distance from }i\text{ to }j

Floyd-Warshall Algorithm

20. Floyd-Warshall Algorithm​

Floyd-Warshall is a dynamic-programming algorithm for the all-pairs shortest-path problem.


21. Floyd-Warshall Recurrence​

Let Dij(k)D^{(k)}_{ij} represent the shortest distance from ii to jj using only the first kk vertices as intermediate vertices.

Then:

Dij(k)=min⁡(Dij(k−1),Dik(k−1)+Dkj(k−1))\boxed{ D^{(k)}_{ij} = \min \left( D^{(k-1)}_{ij}, D^{(k-1)}_{ik} + D^{(k-1)}_{kj} \right) }

22. Floyd-Warshall Complexity​

The time complexity is

O(∣V∣3)\boxed{ O(|V|^3) }

Minimum-Cost Spanning Trees

23. Spanning Tree​

A spanning tree of a connected undirected graph:

  • Contains every vertex.
  • Is connected.
  • Contains no cycles.

For a graph with ∣V∣|V| vertices, every spanning tree contains

∣V∣−1\boxed{|V|-1}

edges.


24. Minimum Spanning Tree​

A Minimum Spanning Tree (MST) is a spanning tree whose total edge weight is minimum.

Its total cost is

∑e∈Tw(e)\sum_{e\in T}w(e)

where TT is the selected spanning tree.


Prim's Algorithm

25. Prim's Algorithm​

Prim's algorithm constructs an MST by repeatedly adding the minimum-weight edge that connects a vertex already in the tree to a vertex outside the tree.


26. Prim's Algorithm​

Starting from any vertex:

  1. Add the starting vertex to the tree.
  2. Find the minimum-weight edge connecting the tree to an unvisited vertex.
  3. Add that edge and vertex.
  4. Repeat until all vertices are included.

27. Prim's Complexity​

Using an adjacency list and priority queue, a common implementation has complexity

O((∣V∣+∣E∣)log⁡∣V∣)O((|V|+|E|)\log |V|)

Kruskal's Algorithm

28. Kruskal's Algorithm​

Kruskal's algorithm constructs an MST by considering edges in increasing order of weight.


29. Kruskal's Algorithm​

  1. Sort all edges by increasing weight.
  2. Consider each edge in that order.
  3. Add the edge if it does not create a cycle.
  4. Continue until ∣V∣−1|V|-1 edges have been selected.

30. Cycle Detection​

Kruskal's algorithm commonly uses a disjoint-set (Union-Find) data structure to determine whether adding an edge would create a cycle.


31. Kruskal's Complexity​

Sorting the edges takes

O(∣E∣log⁡∣E∣)O(|E|\log |E|)

Thus, the overall complexity is commonly written as

O(∣E∣log⁡∣E∣)\boxed{ O(|E|\log |E|) }

Algorithm Comparison

ProblemAlgorithmImportant ConditionComplexity
Single-source shortest pathDijkstraNon-negative edge weights$O((V+E)\logV)$
Single-source shortest pathBellman-FordAllows negative edges$O(VE)$
All-pairs shortest pathsFloyd-Warshall—$O(V^3)$
Minimum spanning treePrimWeighted undirected graph$O((V+E)\logV)$
Minimum spanning treeKruskalWeighted undirected graph$O(E\logE)$

Formula Reference

TopicKey Result
Path weight∑w(e)\displaystyle\sum w(e)
Longest-path relaxationd[v]=max⁡(d[v],d[u]+w(u,v))\displaystyle d[v]=\max(d[v],d[u]+w(u,v))
Transitive closureTij=1T_{ij}=1 if jj is reachable from ii
Matrix multiplicationCij=∑kAikBkj\displaystyle C_{ij}=\sum_kA_{ik}B_{kj}
Relaxationd[v]=min⁡(d[v],d[u]+w(u,v))\displaystyle d[v]=\min(d[v],d[u]+w(u,v))
Dijkstra initializationd[s]=0, d[v]=∞d[s]=0,\ d[v]=\infty
Bellman-Ford passes$V-1$
Bellman-Ford complexity$O(VE)$
Floyd-Warshall recurrenceDij=min⁡(Dij,Dik+Dkj)\displaystyle D_{ij}=\min(D_{ij},D_{ik}+D_{kj})
Floyd-Warshall complexity$O(V^3)$
Edges in spanning tree$V-1$
Prim complexity$O((V+E)\logV)$
Kruskal complexity$O(E\logE)$